package com.gt.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.app.domain.EsDietHabitItem;
import com.gt.app.domain.EsLifeCases;
import com.gt.app.domain.EsUserBaseInfo;
import com.gt.app.domain.dto.EsLifeCasesDto;
import com.gt.app.mapper.EsLifeCasesMapper;
import com.gt.app.service.EsDietHabitItemService;
import com.gt.app.service.EsLifeCasesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 生活状况 服务实现类
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@Service
public class EsLifeCasesServiceImpl extends ServiceImpl<EsLifeCasesMapper, EsLifeCases> implements EsLifeCasesService {

    @Autowired
    private EsDietHabitItemService esDietHabitItemService;


    @Override
    @Transactional
    public void addLifeCases(EsLifeCasesDto esLifeCasesDto) {
        EsLifeCases esLifeCases = new EsLifeCases();
        BeanUtils.copyProperties(esLifeCasesDto,esLifeCases);
        esLifeCases.setUserId(SecurityUtils.getUserId());

        LambdaQueryWrapper<EsLifeCases> wrapper = new LambdaQueryWrapper<EsLifeCases>().eq(EsLifeCases::getUserId, SecurityUtils.getUserId());
        EsLifeCases lifeCases = this.getOne(wrapper);
        if (lifeCases != null){
            this.remove(wrapper);
            LambdaQueryWrapper<EsDietHabitItem> eq = new LambdaQueryWrapper<EsDietHabitItem>().eq(EsDietHabitItem::getLifeCasesId, lifeCases.getId());
            esDietHabitItemService.remove(eq);
        }
        this.save(esLifeCases);
        if (esLifeCasesDto.getDietHabits() != null){
            ArrayList<EsDietHabitItem> esDietHabitItems = new ArrayList<>();
            for (String dietHabit : esLifeCasesDto.getDietHabits()) {
                EsDietHabitItem esDietHabitItem = new EsDietHabitItem();
                esDietHabitItem.setLifeCasesId(esLifeCases.getId());
                esDietHabitItem.setItem(dietHabit);
                esDietHabitItems.add(esDietHabitItem);
            }
            esDietHabitItemService.saveBatch(esDietHabitItems);
        }
    }

    @Override
    public EsLifeCasesDto getLifeCases() {
        EsLifeCasesDto esLifeCasesDto = new EsLifeCasesDto();
        LambdaQueryWrapper<EsLifeCases> wrapper = new LambdaQueryWrapper<EsLifeCases>().eq(EsLifeCases::getUserId, SecurityUtils.getUserId());
        EsLifeCases lifeCase = this.getOne(wrapper);
        BeanUtils.copyProperties(lifeCase,esLifeCasesDto);
        if (lifeCase != null){
            LambdaQueryWrapper<EsDietHabitItem> itemWrapper = new LambdaQueryWrapper<EsDietHabitItem>().eq(EsDietHabitItem::getLifeCasesId, lifeCase.getId());
            List<EsDietHabitItem> list = esDietHabitItemService.list(itemWrapper);
            ArrayList<String> items = new ArrayList<>();
            for (EsDietHabitItem esDietHabitItem : list) {
                items.add(esDietHabitItem.getItem());
            }
            esLifeCasesDto.setDietHabits(items);
        }
        return esLifeCasesDto;
    }
}
